{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 千帆 Hub"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "千帆 SDK 提供了 Hub 能力，可以快速地加载或者保存大模型开发过程中产生的产物，例如 Prompt 等。\n",
    "\n",
    "可以通过如下方式导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qianfan.common import Prompt\n",
    "from qianfan.common.hub import hub"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基于平台的保存 & 加载"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "目前 Prompt 支持从 [平台](https://console.bce.baidu.com/qianfan/prompt/template) 上进行保存和加载，使用方式如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['theme', 'people']\n",
      "('请围绕LLM这一主题写一篇面向开发者的演讲稿，请使用恰当的修辞手法，语言生动，逻辑清晰，引人深思。', None)\n"
     ]
    }
   ],
   "source": [
    "# 从平台拉取 Prompt，格式为 \"<type>/<name>\"\n",
    "# 这里 type 为 prompt，名称为 撰写演讲稿\n",
    "prompt = hub.load(\"prompt/撰写演讲稿\")\n",
    "\n",
    "assert isinstance(prompt, Prompt)\n",
    "print(prompt.variables)\n",
    "print(prompt.render(theme=\"LLM\", people=\"开发者\"))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也可以将本地开发的 Prompt 保存到平台，以便后续快速加载"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('请介绍关于千帆的最新消息', None)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt_name = \"random_新闻介绍\"\n",
    "p = Prompt(\"请介绍关于{target}的最新消息\", name=prompt_name)\n",
    "hub.save(p, to_platform=True)\n",
    "# 或者是更为简洁的\n",
    "hub.push(p)\n",
    "\n",
    "# 通过 json_str 就可以重新加载\n",
    "restored_p = hub.load(\"prompt/\"+prompt_name)\n",
    "restored_p.render(target=\"千帆\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基于文件的保存与加载"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果是平台暂时不支持的类型或者需要保存到本地，可以使用基于文件的保存和加载方式。\n",
    "\n",
    "另外也可以将文件分享给别人，或者是上传至 GitHub 上，共享给其他人使用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('请围绕LLM这一主题写一篇面向开发者的演讲稿，请使用恰当的修辞手法，语言生动，逻辑清晰，引人深思。', None)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = \"./prompt_file\"\n",
    "\n",
    "# 本地开发的 Prompt\n",
    "p = Prompt(\"请介绍关于{target}的最新消息\")\n",
    "# 通过 save 时指定 path 实现保存至文件\n",
    "s = hub.save(p, path=f)\n",
    "\n",
    "# 之后 load 时直接通过 path 加载即可\n",
    "restored_p = hub.load(path=f)\n",
    "\n",
    "# 也可以直接通过 json_str 或 url 加载\n",
    "# restored_p = hub.load(json_str=f)\n",
    "# restored_p = hub.load(url=\"https://...\")\n",
    "\n",
    "# 加载后就可以直接使用\n",
    "restored_p.render(target=\"千帆\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py311",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "f553a591cb5da27fa30e85168a93942a1a24c8d6748197473adb125e5473a5db"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
